package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.constant.HttpResultEnum;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.pojo.itemCat.TbItemCat;
import com.dongdongshop.pojo.typeTemplate.TbTypeTemplate;
import com.dongdongshop.service.itemCat.ItemCatService;
import com.dongdongshop.service.typeTemplate.TypeTemplateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemCat")
public class ItemCatController {

    @Reference
    private ItemCatService itemCatService;

    @Reference
    private TypeTemplateService typeTemplateService;

    private Logger logger = LoggerFactory.getLogger(ItemCatController.class);

    /*
    * 跳转商品分类管理页
    * */
    @RequestMapping("toItemCatList")
    public String toItemCatList(@RequestParam(defaultValue = "0")Long pid, Model model){
        try {
            //获取所有顶级商品分类信息
            List<TbItemCat> itemCats = itemCatService.getItemCatByParentId(pid);
            //获取所有商品类型模板信息
            List<TbTypeTemplate> tts = typeTemplateService.findTypeTemplateAll();
            model.addAttribute("itemCats",itemCats);
            model.addAttribute("tts",tts);
        }catch (Exception e){
            logger.error("跳转商品分类管理页获取所有顶级商品分类信息异常",e);
        }

        return "itemCat/itemCat";
    }
    /*
    * 根据父级id查询商品分类信息
    * */
    @RequestMapping(value = "getItemCatByParentId",method = RequestMethod.POST)
    @ResponseBody
    public DataResult getItemCatByParentId(Long pid){
        List<TbItemCat> itemCats = null;
        try {
            itemCats = itemCatService.getItemCatByParentId(pid);
        }catch (Exception e){
            logger.error("根据父级id查询商品分类信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),itemCats);
    }
    /*
    * 增加商品分类信息
    * */
    @RequestMapping(value = "addItemCat",method = RequestMethod.POST)
    @ResponseBody
    public DataResult addItemCat(TbItemCat ic){
        try {
            itemCatService.addItemCat(ic);
        }catch (Exception e){
            logger.error("增加商品分类异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
    /*
    * 根据分类主键id查询信息
    * */
    @RequestMapping(value = "getItemCatById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult getItemCatById(Long id,Model model){
        TbItemCat tbItemCat = null;
        try {
            tbItemCat = itemCatService.getItemCatById(id);
            model.addAttribute("itemCat",tbItemCat);
        }catch (Exception e){
            logger.error("根据分类主键id查询信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage(),tbItemCat);
    }
    /*
    * 根据商品分类id修改分类信息
    * */
    @RequestMapping(value = "updateItemCatById",method = RequestMethod.POST)
    @ResponseBody
    public DataResult updateItemCatById(TbItemCat ic){
        try {
            itemCatService.updateItemCatById(ic);
        }catch (Exception e){
            logger.error("根据分类id修改分类信息异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
    /*
    * 根据id批量删除商品分类数据 及该分类下面的子分类一起删除
    * */
    @RequestMapping(value = "deleteBatchItemCatByIds",method = RequestMethod.POST)
    @ResponseBody
    public DataResult deleteBatchItemCatByIds(@RequestParam("ids[]")Long[] ids){
        try {
            itemCatService.deleteBatchItemCatByIds(ids);
        }catch (Exception e){
            logger.error("根据id批量删除商品分类数据异常",e);
            return new DataResult(HttpResultEnum.ERROR.getCode(),HttpResultEnum.ERROR.getMessage());
        }
        return new DataResult(HttpResultEnum.SUCCESS.getCode(),HttpResultEnum.SUCCESS.getMessage());
    }
}
